home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connectio…eloper Series 2005 March / Dev.CD Mar 05.iso / What's New / Technical Notes and Q&As / ADC Reference Library / qa / qa2001 / downloads / qa1133_001.hqx / IsUserLoggedIn / IsUserLoggedIn.c < prev   
Encoding:
C/C++ Source or Header  |  2002-04-05  |  5.3 KB  |  132 lines

  1. /*
  2.     File:        IsUserLoggedIn.c
  3.     
  4.     Description:    This program demonstrates how to determine if a console
  5.                         user is currently logged in.
  6.                         Also, the sample demonstrates how to obtain the username
  7.                         of the current console user.
  8.     
  9.     Author:        Chad Jones (chadj@apple.com)
  10.  
  11.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  12.     
  13.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  14.                 ("Apple") in consideration of your agreement to the following terms, and your
  15.                 use, installation, modification or redistribution of this Apple software
  16.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  17.                 please do not use, install, modify or redistribute this Apple software.
  18.  
  19.                 In consideration of your agreement to abide by the following terms, and subject
  20.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  21.                 copyrights in this original Apple software (the "Apple Software"), to use,
  22.                 reproduce, modify and redistribute the Apple Software, with or without
  23.                 modifications, in source and/or binary forms; provided that if you redistribute
  24.                 the Apple Software in its entirety and without modifications, you must retain
  25.                 this notice and the following text and disclaimers in all such redistributions of
  26.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  27.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  28.                 Apple Software without specific prior written permission from Apple.  Except as
  29.                 expressly stated in this notice, no other rights or licenses, express or implied,
  30.                 are granted by Apple herein, including but not limited to any patent rights that
  31.                 may be infringed by your derivative works or by other works in which the Apple
  32.                 Software may be incorporated.
  33.  
  34.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  35.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  36.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  38.                 COMBINATION WITH YOUR PRODUCTS.
  39.  
  40.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  41.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  42.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  44.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  45.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  46.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47.                 
  48.     Change History (most recent first): 
  49. */
  50.  
  51. #include <CoreFoundation/CoreFoundation.h>
  52. #include <SystemConfiguration/SystemConfiguration.h>
  53.  
  54. /* CopyCurrentConsoleUsername
  55.  *
  56.  * This function will return the username of the
  57.  * console user who is currently logged in.  Alternatively
  58.  * this function will return NULL if no user is currently
  59.  * logged into the system.
  60.  */
  61. CFStringRef CopyCurrentConsoleUsername()
  62. {
  63.     CFStringRef    consoleUserName;
  64.     uid_t        uid;
  65.     gid_t        gid;
  66.     
  67.     /* Getting the username of current consoleuser.  Note
  68.      * that if the username is NULL then that means that
  69.      * no user is presently logged in.  We do the lookup
  70.      * using the SCDynamicStoreCopyConsoleUser call.
  71.      * First Argument: The dynamic store to use to lookup
  72.      *     the console username.  We only need a temporary dynamic
  73.      *     store so pass null.
  74.      * Second Argument: On return this will have the
  75.      *     UserID (UID) of the new user.  We pass in a uid_t variable
  76.      *     so we can get the return value.  This value isn't used but
  77.      *     is included for demonstration purposes.
  78.      * Third Argument: On return this will have the
  79.      *     GroupID (GID) of the new user.  We pass in a gid_t variable
  80.      *     so we can get the return value.  This value isn't used but
  81.      *     is included for demonstration purposes.
  82.      * Return Value: The console username expressed as a CFString.
  83.      *     This string must be eventually released with a CFRelease call.
  84.      */
  85.      
  86.     consoleUserName = SCDynamicStoreCopyConsoleUser(NULL, &uid, &gid);
  87.     return(consoleUserName);
  88. }
  89.  
  90. /* ConsoleUserIsLoggedIn
  91.  *
  92.  * This function will return true if a console user is
  93.  * currently logged into the system, and false otherwise.
  94.  */
  95. Boolean ConsoleUserIsLoggedIn()
  96. {
  97.     CFStringRef userName = CopyCurrentConsoleUsername();
  98.     if (userName == NULL)
  99.     {
  100.         //username is NULL thus no user is logged in
  101.         return(FALSE);
  102.     }
  103.     else
  104.     {
  105.         //username is non-NULL thus a console user is present
  106.         CFRelease(userName); //release username
  107.         return(TRUE);
  108.     }
  109. }
  110.  
  111. int main()
  112. {
  113.     Boolean result;
  114.     
  115.     result = ConsoleUserIsLoggedIn();
  116.     
  117.     if (result == TRUE)
  118.     {
  119.         printf("A console user is currently logged in\n");
  120.  
  121.         //Getting and printing the current username.
  122.         printf("Current User: \t"); fflush(stdout);
  123.         CFShow(CopyCurrentConsoleUsername());
  124.     }
  125.     else
  126.     {
  127.         printf("No user is currently logged in");
  128.     }
  129.     
  130.     return(0);
  131. }
  132.